$(function () {
    //获得权限列表
    function getTable() {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Admin/Permission?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/Permission?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                // var result = JSON.stringify(data);
                if (data.code == 1) {
                    $("#powerTable tbody tr").remove();
                    console.log(data);
                    var count = data.data.length;
                    for (var i = 0; i < count; i++) {
                        var addtime = data.data[i].addtime || '';
                        if (addtime != '') {
                            addtime = getMyDate(addtime)
                        }
                        ;
                        var id = data.data[i].id || '';
                        var updatetime = data.data[i].updatetime || '';
                        if (updatetime != '') {
                            updatetime = getMyDate(updatetime)
                        }
                        var name = data.data[i].name || '';
                        var parentid = data.data[i].parentid || '';
                        var parentids = data.data[i].parentids || '';
                        var permission = data.data[i].permission || '';
                        var resourcetype = data.data[i].resourcetype || '';
                        var url = data.data[i].url || '';

                        var tr = "<tr _id=" + id + " _parentid = " + parentid + "><td>" + name + "</td><td>" + resourcetype + "</td><td>" + url + "</td><td>" + permission + "</td><td><button class='powerTable_updata _updata'><i class='icon-edit'>&nbsp;</i>修改</button><button class='powerTable_delect _delect'><i class='icon-trash'>&nbsp;</i>删除</button></td></tr>";
                        $("#powerTable tbody").append(tr);
                        updateAndDel();
                    }
                }
            },
            error: function () {
                alert('获取权限列表失败');
            }
        });
    }

    getTable();

    // 添加 修改权限
    function updateAndDel() {
        // 修改权限
        $(".powerTable_updata").click(function () {
            $(".update_div").attr({"permissionId":$(this).parent().parent().attr("_id")});
            $("#updateName").val($(this).parent().parent().find("td").eq(0).text());
            $("#updateUrl").val($(this).parent().parent().find("td").eq(2).text());
            $(".update_div").fadeIn();
        });
        //删除权限
        $(".powerTable_delect").click(function () {
            var _timestamp = new Date().getTime();
            var _secret = hex_md5(ip + '/Back/Admin/DelPermission?timestamp=' + _timestamp + salt);
            var _id = parseInt($(this).parent().parent().attr("_id"));
            var that = $(this).parent().parent();
            $.ajax({
                type: "POST",
                url: ip + '/Back/Admin/DelPermission?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
                dataType: 'json',
                data: JSON.stringify({"id": _id}),
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json;charset=utf-8",
                    // "token":_token
                },
                success: function (data) {
                    // var result = JSON.stringify(data);
                    if (data.code == 1) {
                        that.fadeOut();
                    }
                    else {
                        alert(data.message)
                    }
                },
                error: function () {
                    alert('删除失败');
                },
            });
        })
    }

    $("#addPower").click(function () {
        $(".add_div").fadeToggle();
    });
    $("#addPowerN").click(function () {
        $(".add_div").fadeOut()
    });
    $("#updatePowerN").click(function () {
        $(".update_div").fadeOut()
    });
    //添加权限
    $("#addPowerY").click(function () {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Admin/AddPermission?timestamp=' + _timestamp + salt);
        var _name = $("#addName").val();
        var _permission = $("#addPermission").val();
        var _resourceType = $("#addResourceType option:selected").val();
        var _url = $("#addUrl").val();
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/AddPermission?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            data: JSON.stringify({
                "name": _name,
                "parentId": 0,
                "parentIds": "0",
                "permission": _permission,
                "resourceType": _resourceType,
                "url": _url
            }),
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                // "token":_token
            },
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    getTable();
                    $(".add_div").fadeOut();
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('删除失败');
            },
        });
    });
// 修改权限
    $("#updatePowerY").click(function () {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Admin/UpdatePermission?timestamp=' + _timestamp + salt);
        var _name = $("#updateName").val();
        var _permissionId  = $(".update_div").attr("permissionid");
        var _url = $("#updateUrl").val();
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/UpdatePermission?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            data: JSON.stringify({
                "name": _name,
                "permissionId":_permissionId,
                "url": _url
            }),
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
                // "token":_token
            },
            success: function (data) {
                // var result = JSON.stringify(data);
                console.log(data)
                if (data.code == 1) {
                    getTable();
                    $(".update_div").fadeOut();
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('删除失败');
            },
        });
    });
});